// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package com.kyexpress.vms.configsetting;

import com.intellij.openapi.options.Configurable;
import com.kyexpress.vms.component.AppSettingsComponent;
import com.kyexpress.vms.enitity.MyConfig;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * Provides controller functionality for application settings.
 */
public class AppSettingsConfigurable implements Configurable {

  private AppSettingsComponent mySettingsComponent;

  // A default constructor with no arguments is required because this implementation
  // is registered as an applicationConfigurable EP

  @Nls(capitalization = Nls.Capitalization.Title)
  @Override
  public String getDisplayName() {
    return "Dict Synchronizer Setting";
  }

  @Override
  public JComponent getPreferredFocusedComponent() {
    return mySettingsComponent.getPreferredFocusedComponent();
  }

  @Nullable
  @Override
  public JComponent createComponent() {
    mySettingsComponent = new AppSettingsComponent();
    return mySettingsComponent.getPanel();
  }

  @Override
  public boolean isModified() {
    MyConfig settings = MyConfig.getInstance();
    boolean modified = !mySettingsComponent.getApp_id().equals(settings.app_id);
    modified |= mySettingsComponent.getApp_secret() != settings.app_secret;
    modified |= mySettingsComponent.getAuthor() != settings.author;
    modified |= mySettingsComponent.getDictUrl() != settings.dict_host;
    modified |= mySettingsComponent.getPackageName() != settings.package_name;
    modified |= mySettingsComponent.getModuleName() != settings.module_name;
    modified |= mySettingsComponent.getDict_select() != settings.dict_select;
    return modified;
  }

  @Override
  public void apply() {
    MyConfig settings = MyConfig.getInstance();
    settings.dict_host = mySettingsComponent.getDictUrl();
    settings.package_name = mySettingsComponent.getPackageName();
    settings.author = mySettingsComponent.getAuthor();
    settings.app_id = mySettingsComponent.getApp_id();
    settings.app_secret = mySettingsComponent.getApp_secret();
    settings.module_name = mySettingsComponent.getModuleName();
    settings.dict_select = mySettingsComponent.getDict_select();
  }

  @Override
  public void reset() {
    MyConfig settings = MyConfig.getInstance();
    mySettingsComponent.setApp_id(settings.app_id);
    mySettingsComponent.setApp_secret(settings.app_secret);
    mySettingsComponent.setAuthor(settings.author);
    mySettingsComponent.setDictUrl(settings.dict_host);
    mySettingsComponent.setPackageName(settings.package_name);
    mySettingsComponent.setModuleName(settings.module_name);
    mySettingsComponent.setDict_select(settings.dict_select);
  }

  @Override
  public void disposeUIResources() {
    mySettingsComponent = null;
  }

}
